﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NguoiDung
{
    public partial class ViecDaUngTuyen : System.Web.UI.Page
    {
        public int Trang { set; get; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["page"] != null)
            {
                Trang = int.Parse(Request.QueryString["page"]);
            }
            else
            {
                Trang = 1;
            }
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if(Session["NguoiDung"]!=null)
            {
                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                if(user!=null)
                {
                    int page = 1;

                    if (Request.QueryString["page"] != null)
                    {
                        page = int.Parse(Request.QueryString["page"]);
                    }
                    string path = HttpContext.Current.Request.Url.AbsolutePath;

                    var data = BLL.NguoiDung.TuyenDung.ViecDaUngTuyen(user.UserId);
                    rptViecDaUngTuyen.DataSource = data.Skip((page - 1) * 10).Take(10);
                    rptViecDaUngTuyen.DataBind();

                    int Count = data.Count();
                    int TotalPage = Count / 10 + 1;
                    if (Count < 1)
                    {
                        lbtPreview.Visible = false;
                        lbtNext.Visible = false;
                    }
                    if (page == 1)
                    {
                        lbtPreview.Visible = false;
                    }
                    if (page == TotalPage)
                    {
                        lbtNext.Visible = false;
                    }
                    for (int i = page - 4; i < page + 4; i++)
                    {
                        if (i < 1)
                            continue;
                        if (i > TotalPage)
                            continue;
                        if (i == page && Count > 0)
                            ltlPaging.Text += "<li><a class='current' href='" + path + "?page=" + i + "'>" + i + "</a></li>";
                        else
                            ltlPaging.Text += "<li><a href='" + path + "?page=" + i + "'>" + i + "</a></li>";
                    }


                }
            }
        }

        protected void lbtPreview_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?page=" + (Trang + 1));
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?page=" + (Trang + 1));
        }
    }
}